package idv.loveloser.mobilemoodle;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

import android.os.StrictMode;

public class Util {
	public static String login(String username,String password){
		StrictMode.enableDefaults();
		String responseStr="";
		HttpPost post = new HttpPost(Constances.URL+Constances.login);
		HttpClient client = new DefaultHttpClient();
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("username",username));
		params.add(new BasicNameValuePair("password",password));
		try{
			post.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8));
			HttpResponse response =client.execute(post);
			if(response.getStatusLine().getStatusCode()==200){
				responseStr=EntityUtils.toString(response.getEntity());
				
			}
				
		}catch (UnsupportedEncodingException e) {
			
			e.printStackTrace();
			return "";
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "";
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "";
		}
		return responseStr;
	}
	public static final String getAllCourse(long clientLong,String sessionKey){
		StrictMode.enableDefaults();
		String responseStr="";
		String url=Constances.URL+Constances.getAllCourses+"?client="+clientLong+"&sessionKey="+sessionKey;
		HttpGet get = new HttpGet(url);
		HttpClient client = new DefaultHttpClient();
		
		try{
//			get.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8));
			HttpResponse response =client.execute(get);
			if(response.getStatusLine().getStatusCode()==200){
				responseStr=EntityUtils.toString(response.getEntity());
				
			}
				
		}catch (UnsupportedEncodingException e) {
			
			e.printStackTrace();
			return "";
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "";
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "";
		}
		return responseStr;
	}
	public static final String getCategories(long clientLong,String sessionKey){
		StrictMode.enableDefaults();
		String responseStr="";
		String url=Constances.URL+Constances.getAllCategories+"?client="+clientLong+"&sessionKey="+sessionKey;
		HttpGet get = new HttpGet(url);
		HttpClient client = new DefaultHttpClient();
		
		try{
//			get.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8));
			HttpResponse response =client.execute(get);
			if(response.getStatusLine().getStatusCode()==200){
				responseStr=EntityUtils.toString(response.getEntity());
				
			}
				
		}catch (UnsupportedEncodingException e) {
			
			e.printStackTrace();
			return "";
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "";
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "";
		}
		return responseStr;
	}
	public static final String addCourse(long clientLong,String sessionKey,String shortname,String fullname,long categoryId){
		StrictMode.enableDefaults();
		String responseStr="";
		HttpPost post = new HttpPost(Constances.URL+Constances.addCourse);
		HttpClient client = new DefaultHttpClient();
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("client",clientLong+""));
		params.add(new BasicNameValuePair("sessionKey",sessionKey));
		params.add(new BasicNameValuePair("shortname",shortname));
		params.add(new BasicNameValuePair("fullname",fullname));
		params.add(new BasicNameValuePair("category",categoryId+""));
		try{
			post.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8));
			HttpResponse response =client.execute(post);
			if(response.getStatusLine().getStatusCode()==200){
				responseStr=EntityUtils.toString(response.getEntity());				
			}
				
		}catch (UnsupportedEncodingException e) {
			
			e.printStackTrace();
			return "";
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "";
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "";
		}
		return responseStr;
		
	}
	public static final String getAllAssignmnets(long clientLong,String sessionKey){
		StrictMode.enableDefaults();
	
		String responseStr="";
		String url=Constances.URL+Constances.getAllAssignments+"?client="+clientLong+"&sessionKey="+sessionKey;
		HttpGet get = new HttpGet(url);
		HttpClient client = new DefaultHttpClient();
		
		try{
	//		get.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8));
			HttpResponse response =client.execute(get);
			if(response.getStatusLine().getStatusCode()==200){
				responseStr=EntityUtils.toString(response.getEntity());
				
			}
				
		}catch (UnsupportedEncodingException e) {
			
			e.printStackTrace();
			return "";
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "";
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "";
		}
		return responseStr;
	}
	public static final String addAssignment(long clientLong,String sessionKey,String grade,String intro,String name,boolean resubmit,int dueDays,long courseId){
		StrictMode.enableDefaults();
		String responseStr="";
		HttpPost post = new HttpPost(Constances.URL+Constances.addAssignment);
		HttpClient client = new DefaultHttpClient();
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("client",clientLong+""));
		params.add(new BasicNameValuePair("sessionKey",sessionKey));
		params.add(new BasicNameValuePair("grade",grade));
		params.add(new BasicNameValuePair("intro",intro));
		params.add(new BasicNameValuePair("name",name));
		params.add(new BasicNameValuePair("courseId",courseId+""));
		if(resubmit)
			params.add(new BasicNameValuePair("resubmit","1"));
		else
			params.add(new BasicNameValuePair("resubmit","0"));
		params.add(new BasicNameValuePair("due_days",dueDays+""));
		try{
			post.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8));
			HttpResponse response =client.execute(post);
			if(response.getStatusLine().getStatusCode()==200){
				responseStr=EntityUtils.toString(response.getEntity());				
			}
				
		}catch (UnsupportedEncodingException e) {
			
			e.printStackTrace();
			return "";
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "";
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "";
		}
		return responseStr;
	}
	public static final String getCourseContent(long clientLong,String sessionKey,long courseId){
		StrictMode.enableDefaults();
		String responseStr="";
		String url=Constances.URL+Constances.getCourseContent+"?client="+clientLong+"&sessionKey="+sessionKey+"&courseId="+courseId;
		HttpGet get = new HttpGet(url);
		HttpClient client = new DefaultHttpClient();
		
		try{
	//		get.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8));
			HttpResponse response =client.execute(get);
			if(response.getStatusLine().getStatusCode()==200){
				responseStr=EntityUtils.toString(response.getEntity());
				
			}
				
		}catch (UnsupportedEncodingException e) {
			
			e.printStackTrace();
			return "";
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "";
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "";
		}
		return responseStr;
	}
	public static final String getCourseSection(long clientLong,String sessionKey,long courseId){
		StrictMode.enableDefaults();
		String responseStr="";
		String url=Constances.URL+Constances.getSections+"?client="+clientLong+"&sessionKey="+sessionKey+"&courseId="+courseId;
		HttpGet get = new HttpGet(url);
		HttpClient client = new DefaultHttpClient();
		
		try{
	//		get.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8));
			HttpResponse response =client.execute(get);
			if(response.getStatusLine().getStatusCode()==200){
				responseStr=EntityUtils.toString(response.getEntity());
				
			}
				
		}catch (UnsupportedEncodingException e) {
			
			e.printStackTrace();
			return "";
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "";
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "";
		}
		return responseStr;
	}
	public static final String getCourseSectionCount(long clientLong,String sessionKey,long courseId){
		StrictMode.enableDefaults();
		String responseStr="";
		String url=Constances.URL+Constances.getCourseSectionCount+"?client="+clientLong+"&sessionKey="+sessionKey+"&courseId="+courseId;
		HttpGet get = new HttpGet(url);
		HttpClient client = new DefaultHttpClient();
		
		try{
	//		get.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8));
			HttpResponse response =client.execute(get);
			if(response.getStatusLine().getStatusCode()==200){
				responseStr=EntityUtils.toString(response.getEntity());
				
			}
				
		}catch (UnsupportedEncodingException e) {
			
			e.printStackTrace();
			return "";
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "";
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "";
		}
		return responseStr;
	}
	public static final String getPostsSubjectsInForum(long clientLong,String sessionKey,long courseId,long forumId){
		StrictMode.enableDefaults();
		String responseStr="";
		String url=Constances.URL+Constances.getPostsSubjectsInForum+"?client="+clientLong+"&sessionKey="+sessionKey+"&courseId="+courseId+"&forumId="+forumId;
		HttpGet get = new HttpGet(url);
		HttpClient client = new DefaultHttpClient();
		
		try{
	//		get.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8));
			HttpResponse response =client.execute(get);
			if(response.getStatusLine().getStatusCode()==200){
				responseStr=EntityUtils.toString(response.getEntity());
				
			}
				
		}catch (UnsupportedEncodingException e) {
			
			e.printStackTrace();
			return "";
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "";
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "";
		}
		return responseStr;
	}
	public static final String getUsernameById(long clientLong,String sessionKey,long userId){
		StrictMode.enableDefaults();
		String responseStr="";
		String url=Constances.URL+Constances.getUsernameById+"?client="+clientLong+"&sessionKey="+sessionKey+"&userId="+userId;
		HttpGet get = new HttpGet(url);
		HttpClient client = new DefaultHttpClient();
		
		try{
	//		get.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8));
			HttpResponse response =client.execute(get);
			if(response.getStatusLine().getStatusCode()==200){
				responseStr=EntityUtils.toString(response.getEntity());
				
			}
				
		}catch (UnsupportedEncodingException e) {
			
			e.printStackTrace();
			return "";
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "";
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "";
		}
		return responseStr;
	}
	public static final String getPostsList(long clientLong,String sessionKey,long discussionId){
		StrictMode.enableDefaults();
		String responseStr="";
		String url=Constances.URL+Constances.getPostsList+"?client="+clientLong+"&sessionKey="+sessionKey+"&discussionId="+discussionId;
		HttpGet get = new HttpGet(url);
		HttpClient client = new DefaultHttpClient();
		
		try{
	//		get.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8));
			HttpResponse response =client.execute(get);
			if(response.getStatusLine().getStatusCode()==200){
				responseStr=EntityUtils.toString(response.getEntity());				
			}
				
		}catch (UnsupportedEncodingException e) {
			
			e.printStackTrace();
			return "";
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "";
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "";
		}
		return responseStr;
	}
	public static String postInFrum(long clientLong,String sessionKey,long courseId,long forumId,long parent,String message,String subject){
		StrictMode.enableDefaults();
		String responseStr="";
		HttpPost post = new HttpPost(Constances.URL+Constances.postInForum);
		HttpClient client = new DefaultHttpClient();
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("client",clientLong+""));
		params.add(new BasicNameValuePair("sessionKey",sessionKey));
		params.add(new BasicNameValuePair("courseId",courseId+""));
		params.add(new BasicNameValuePair("forumId",forumId+""));
		params.add(new BasicNameValuePair("parent",parent+""));
		params.add(new BasicNameValuePair("message",message));
		params.add(new BasicNameValuePair("subject",subject));
		try{
			post.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8));
			HttpResponse response =client.execute(post);
			if(response.getStatusLine().getStatusCode()==200){
				responseStr=EntityUtils.toString(response.getEntity());
				
			}
				
		}catch (UnsupportedEncodingException e) {
			
			e.printStackTrace();
			return "";
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "";
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "";
		}
		return responseStr;
	}
	public static String getCourseUrl(long clientLong,String sessionKey,long urlId){
		StrictMode.enableDefaults();
		String responseStr="";
		String url=Constances.URL+Constances.getCourseUrl+"?client="+clientLong+"&sessionKey="+sessionKey+"&urlId="+urlId;
		HttpGet get = new HttpGet(url);
		HttpClient client = new DefaultHttpClient();
		
		try{
	//		get.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8));
			HttpResponse response =client.execute(get);
			if(response.getStatusLine().getStatusCode()==200){
				responseStr=EntityUtils.toString(response.getEntity());				
			}
				
		}catch (UnsupportedEncodingException e) {
			
			e.printStackTrace();
			return "";
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "";
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "";
		}
		return responseStr;
	}
	public static String getAllForums(long clientLong,String sessionKey){
		StrictMode.enableDefaults();
		String responseStr="";
		String url=Constances.URL+Constances.getAllForums+"?client="+clientLong+"&sessionKey="+sessionKey;
		HttpGet get = new HttpGet(url);
		HttpClient client = new DefaultHttpClient();
		
		try{
	//		get.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8));
			HttpResponse response =client.execute(get);
			if(response.getStatusLine().getStatusCode()==200){
				responseStr=EntityUtils.toString(response.getEntity());				
			}
				
		}catch (UnsupportedEncodingException e) {
			
			e.printStackTrace();
			return "";
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "";
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "";
		}
		return responseStr;
	}
	public static final String addForum(long clientLong,String sessionKey,String intro,String name,long sectionId, long courseId){
		StrictMode.enableDefaults();
		String responseStr="";
		HttpPost post = new HttpPost(Constances.URL+Constances.addNewForum);
		HttpClient client = new DefaultHttpClient();
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("client",clientLong+""));
		params.add(new BasicNameValuePair("sessionKey",sessionKey));
		params.add(new BasicNameValuePair("forum_description",intro));
		params.add(new BasicNameValuePair("forum_name",name));
		params.add(new BasicNameValuePair("sectionId",sectionId+""));
		params.add(new BasicNameValuePair("courseId",courseId+""));

		try{
			post.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8));
			HttpResponse response =client.execute(post);
			if(response.getStatusLine().getStatusCode()==200){
				responseStr=EntityUtils.toString(response.getEntity());				
			}
				
		}catch (UnsupportedEncodingException e) {
			
			e.printStackTrace();
			return "";
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "";
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "";
		}
		return responseStr;
	}
	public static String getForumListInCourse(long clientLong,String sessionKey,long courseId){
		StrictMode.enableDefaults();
		String responseStr="";
		String url=Constances.URL+Constances.getForumListInCourse+"?client="+clientLong+"&courseId="+courseId+"&sessionKey="+sessionKey;
		HttpGet get = new HttpGet(url);
		HttpClient client = new DefaultHttpClient();
		
		try{
	//		get.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8));
			HttpResponse response =client.execute(get);
			if(response.getStatusLine().getStatusCode()==200){
				responseStr=EntityUtils.toString(response.getEntity());				
			}
				
		}catch (UnsupportedEncodingException e) {
			
			e.printStackTrace();
			return "";
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "";
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "";
		}
		return responseStr;
	}
	public static String replyPostInFrum(long clientLong,String sessionKey,long courseId,long forumId,long parent,String message,String subject,long discussionId){
		StrictMode.enableDefaults();
		String responseStr="";
		HttpPost post = new HttpPost(Constances.URL+Constances.replyPostForumInCourse);
		HttpClient client = new DefaultHttpClient();
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("client",clientLong+""));
		params.add(new BasicNameValuePair("sessionKey",sessionKey));
		params.add(new BasicNameValuePair("courseId",courseId+""));
		params.add(new BasicNameValuePair("forumId",forumId+""));
		params.add(new BasicNameValuePair("parent",parent+""));
		params.add(new BasicNameValuePair("message",message));
		params.add(new BasicNameValuePair("subject",subject));
		params.add(new BasicNameValuePair("discussionId",discussionId+""));
		try{
			post.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8));
			HttpResponse response =client.execute(post);
			if(response.getStatusLine().getStatusCode()==200){
				responseStr=EntityUtils.toString(response.getEntity());
				
			}
				
		}catch (UnsupportedEncodingException e) {
			
			e.printStackTrace();
			return "";
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "";
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "";
		}
		return responseStr;
	}
	public static final String addCourseUrl(long clientLong,String sessionKey,long sectionId,long courseId,String urlName,String url,String intro){
		StrictMode.enableDefaults();
		String responseStr="";
		HttpPost post = new HttpPost(Constances.URL+Constances.addCourseURL);
		HttpClient client = new DefaultHttpClient();
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("client",clientLong+""));
		params.add(new BasicNameValuePair("sessionKey",sessionKey));
		params.add(new BasicNameValuePair("sectionId",sectionId+""));
		params.add(new BasicNameValuePair("courseId",courseId+""));
		params.add(new BasicNameValuePair("url_name",urlName));
		params.add(new BasicNameValuePair("url",url));
		params.add(new BasicNameValuePair("intro",intro));
		try{
			post.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8));
			HttpResponse response =client.execute(post);
			if(response.getStatusLine().getStatusCode()==200){
				responseStr=EntityUtils.toString(response.getEntity());				
			}
				
		}catch (UnsupportedEncodingException e) {
			
			e.printStackTrace();
			return "";
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "";
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "";
		}
		return responseStr;
		
	}
	public static final String getAssignmentGrade(long clientLong,String sessionKey,long courseId,long assignmentId){
		StrictMode.enableDefaults();
		String responseStr="";
		String url=Constances.URL+Constances.getAssignmentGrade+"?client="+clientLong+"&courseId="+courseId+"&assignmentId="+assignmentId+"&sessionKey="+sessionKey;
		HttpGet get = new HttpGet(url);
		HttpClient client = new DefaultHttpClient();
		
		try{
//			get.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8));
			HttpResponse response =client.execute(get);
			if(response.getStatusLine().getStatusCode()==200){
				responseStr=EntityUtils.toString(response.getEntity());
				
			}
				
		}catch (UnsupportedEncodingException e) {
			
			e.printStackTrace();
			return "";
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "";
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "";
		}
		return responseStr;
	}
	public static final String uploadAssignment(long clientLong,String sessionKey,long assignmentId,String assignmentMessage){
		StrictMode.enableDefaults();
		String responseStr="";
		HttpPost post = new HttpPost(Constances.URL+Constances.uploadAssignment);
		HttpClient client = new DefaultHttpClient();
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("client",clientLong+""));
		params.add(new BasicNameValuePair("sessionKey",sessionKey));
		params.add(new BasicNameValuePair("assignmentId",assignmentId+""));
		params.add(new BasicNameValuePair("content",assignmentMessage));

		try{
			post.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8));
			HttpResponse response =client.execute(post);
			if(response.getStatusLine().getStatusCode()==200){
				responseStr=EntityUtils.toString(response.getEntity());				
			}
				
		}catch (UnsupportedEncodingException e) {
			
			e.printStackTrace();
			return "";
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "";
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "";
		}
		return responseStr;
		
	}
}
